﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.IO;

namespace AliCMS.VoteModule.Bll
{
    public partial class Vote : DbObjectBase, IVote
    {
        public class Option : DbObjectBase, VoteModule.Vote.IOption
        {
            public Option(int instanceID, string lang, bool checkRight, string database)
                : base(instanceID, lang, checkRight, database)
            {
            }

            public override object ExecuteService(HttpContext context)
            {
                object result = null;

                string operation = context.Request.QueryString["Operation"];
                if (operation == "Add")
                {
                    Dictionary<string, object> item = new Dictionary<string, object>();
                    string value = null;
                    item[Alias.Vote.Option.VoteID] = context.Request.Form["VoteID"];
                    value = context.Request.Form["Name"];
                    if (value != null) item[Alias.Vote.Option.Name] = value;
                    value = context.Request.Form["Content"];
                    if (value != null) item[Alias.Vote.Option.Content] = value;
                    value = context.Request.Form["Picture"];
                    if (value != null) item[Alias.Vote.Option.Picture] = value;
                    value = context.Request.Form["Sort"];
                    if (value != null) item[Alias.Vote.Option.Sort] = value;

                    result = Add(item);
                }
                else if (operation == "Modify")
                {
                    int id = Convert.ToInt32(context.Request.QueryString["ID"]);

                    Dictionary<string, object> changes = new Dictionary<string, object>();
                    string value = null;
                    value = context.Request.Form["Name"];
                    if (value != null) changes[Alias.Vote.Option.Name] = value;
                    value = context.Request.Form["Content"];
                    if (value != null) changes[Alias.Vote.Option.Content] = value;
                    value = context.Request.Form["Picture"];
                    if (value != null) changes[Alias.Vote.Option.Picture] = value;
                    value = context.Request.Form["Sort"];
                    if (value != null) changes[Alias.Vote.Option.Sort] = value;

                    Modify(id, changes);
                }
                else if (operation == "Delete")
                {
                    string id = context.Request.QueryString["ID"];

                    string[] ids = id.Split('|');
                    for (int i = 0; i < ids.Length; i++)
                    {
                        if (String.IsNullOrEmpty(ids[i]))
                            continue;

                        Delete(Convert.ToInt32(ids[i]));
                    }
                }
                else if (operation == "List")
                {
                    string value = null;

                    int page = -1;
                    value = context.Request.QueryString["Page"];
                    if (value != null) page = Convert.ToInt32(value);

                    int pageSize = -1;
                    value = context.Request.QueryString["PageSize"];
                    if (value != null) pageSize = Convert.ToInt32(value);

                    string condition = context.Request.QueryString["Condition"];
                    string sort = context.Request.QueryString["Sort"];

                    result = List(page, pageSize, condition, sort);
                }
                else if (operation == "GetInnerIDFromGuid")
                {
                    Guid guid = new Guid(context.Request.QueryString["ID"]);
                    result = GetInnerIDFromGuid(guid);
                }
                else
                {
                    throw new Exception("lang_header_param");
                }

                return result;
            }
            public override void AliUpload(string fieldName, ref string uploadFile, bool isLast)
            {
                string operation = HttpContext.Current.Request.QueryString["Operation"];
                if (operation == "UploadPicture")
                    UploadPicture(fieldName, ref uploadFile, isLast);
                else
                    throw new Exception("lang_header_param");
            }

            public int GetInnerIDFromGuid(Guid guid)
            {
                return Dal.GetInnerIDFromGuid(guid);
            }
            public Guid GetGuidFromInnerID(object id)
            {
                return Dal.GetGuidFromInnerID(id);
            }
            public Dictionary<string, object> Get(object id)
            {
                if (CheckRight)
                {
                    Guid guid;
                    if (id is Guid)
                        guid = (Guid)id;
                    else
                        guid = Dal.GetGuidFromInnerID(id);
                    ValidateRight("Get", guid);
                }

                return Dal.Get(id);
            }
            public Guid Add(Dictionary<string, object> item)
            {
                if (CheckRight)
                {
                    ValidateRight("Add", Guid.Empty);
                }

                if (!item.ContainsKey(Alias.Vote.Option.ID) || item[Alias.Vote.Option.ID].ToString() == String.Empty)
                    item[Alias.Vote.Option.ID] = Guid.NewGuid();
                else if (!(item[Alias.Vote.Option.ID] is Guid))
                    item[Alias.Vote.Option.ID] = new Guid(item[Alias.Vote.Option.ID].ToString());

                if (!(item[Alias.Vote.Option.VoteID] is Guid))
                    item[Alias.Vote.Option.VoteID] = new Guid(item[Alias.Vote.Option.VoteID].ToString());

                Dal.Add(item);

                return (Guid)item[Alias.Vote.Option.ID];
            }
            public void Modify(int id, Dictionary<string, object> changes)
            {
                if (CheckRight)
                {
                    Guid guid = Dal.GetGuidFromInnerID(id);
                    ValidateRight("Modify", guid);
                }

                Dal.Modify(id, changes);
            }
            public void Delete(int id)
            {
                if (CheckRight)
                {
                    Guid guid = Dal.GetGuidFromInnerID(id);
                    ValidateRight("Delete", guid);
                }

                Dal.Delete(id);
            }
            public void Move(int from, int to)
            {
                if (CheckRight)
                {
                    ValidateRight("Move", Guid.Empty);
                }

                Dal.Move(from, to);
            }
            public DataTable List(int page, int pageSize, string condition, string sort)
            {
                if (CheckRight)
                {
                    ValidateRight("List", Guid.Empty);
                }

                return Dal.List(page, pageSize, condition, sort);
            }
            public int GetRecordCount(string condition)
            {
                return Dal.GetRecordCount(condition);
            }

            public void UploadPicture(string fieldName, ref string uploadFile, bool isLast)
            {
                if (CheckRight)
                {
                    ValidateRight("UploadPicture", Guid.Empty);
                }

                HttpRequest request = HttpContext.Current.Request;
                int id = Convert.ToInt32(request.QueryString["ID"]);

                if (!isLast)
                {
                    string path = String.Empty;

                    path += Manager.GetDataPath(InstanceID, Lang) + "picture/";
                    Manager.MakeSureFolderExist(Manager.MapPath(path));

                    if (Convert.ToBoolean(request.QueryString["UploadFileNameBySystem"]))
                        uploadFile = path + id + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetExtension(uploadFile);
                    else
                        uploadFile = path + uploadFile;
                }
                else
                {
                    #region 删除原有的文件
                    Dictionary<string, object> item = Dal.Get(id);

                    string file = item[Alias.Vote.Option.Picture].ToString();
                    if (!file.Equals(uploadFile, StringComparison.InvariantCultureIgnoreCase) && file.StartsWith("/") && File.Exists(Manager.MapPath(file)))
                        File.Delete(Manager.MapPath(file));
                    #endregion

                    if (Convert.ToBoolean(request.QueryString["AddWaterMark"]))
                        Manager.AddWaterMark(Manager.MapPath(uploadFile), Lang);

                    Dictionary<string, object> changes = new Dictionary<string, object>();
                    changes[Alias.Vote.Option.Picture + Lang] = uploadFile;

                    Dal.Modify(id, changes);
                }
            }

            #region Properties
            private Dal.Vote.IOption Dal
            {
                get { return (Dal.Vote.IOption)DalBase; }
            }
            #endregion
        }
    }
}
